package com.gitee.liang521.generate.api.service.impl;

import cn.hutool.core.util.StrUtil;
import com.gitee.liang521.generate.api.config.GenerateProperties;
import com.gitee.liang521.generate.api.generate.GenerateFactory;
import com.gitee.liang521.generate.api.pojo.dto.TableInfoDto;
import com.gitee.liang521.generate.api.pojo.entity.ColumnEntity;
import com.gitee.liang521.generate.api.pojo.entity.TableEntity;
import com.gitee.liang521.generate.api.pojo.maps.ColumnMaps;
import com.gitee.liang521.generate.api.pojo.vo.generate.GenerateReqVo;
import com.gitee.liang521.generate.api.pojo.vo.generate.ListResVo;
import com.gitee.liang521.generate.api.service.ColumnService;
import com.gitee.liang521.generate.api.service.GenerateService;
import com.gitee.liang521.generate.api.service.TableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author liang zai
 * @date 2021/9/16 15:05
 * @description
 */
@Service
@Slf4j
public class GenerateServiceImpl implements GenerateService {

    @Resource
    private TableService tableService;

    @Resource
    private GenerateProperties generateProperties;

    @Resource
    private ColumnService columnService;

    @Resource
    private ColumnMaps columnMaps;

    @Override
    public List<ListResVo> listQuery() {
        return tableService.listQuery();
    }

    @Override
    public List<String> types() {
        return new ArrayList<>(generateProperties.getTemplateMap().keySet());
    }

    @Override
    public void generate(GenerateReqVo generateReqVo) {
        //获取表结构
        List<ColumnEntity> columnEntities = columnService.getByTableNames(generateReqVo.getTableNames());
        Map<String, List<ColumnEntity>> tableNameMap = columnEntities.stream().collect(Collectors.groupingBy(ColumnEntity::getTableName));
        List<TableInfoDto> tableInfos = new ArrayList<>();
        tableNameMap.forEach((k,v)->{
            TableEntity tableEntity = tableService.getByName(k);
            if (tableEntity == null) {
                return;
            }
            tableInfos.add(TableInfoDto.builder()
                    .tableName(tableEntity.getTableName())
                    .tableNameCamelCase(StrUtil.upperFirst(StrUtil.toCamelCase(tableEntity.getTableName())))
                    .tableComment(tableEntity.getTableComment())
                    .columnDtos(columnMaps.poToDto(v))
                    .build());
        });

        //代码生成
        tableInfos.forEach(tableInfo -> {
            generateReqVo.getTypes().forEach(type ->{
                if (GenerateFactory.get(generateProperties.getTemplateMap().get(type).getType()) == null){
                    throw new RuntimeException("未定义的生成器类型["+generateProperties.getTemplateMap().get(type).getType() +
                            "]");
                }
                //生成
                GenerateProperties.Template template = generateProperties.getTemplateMap().get(type);
                GenerateFactory.get(template.getType()).generate(tableInfo, template, generateReqVo.getModule());
                //递归生成字模板
                if (template.getChild() != null) {
                    template.getChild().forEach((k,v)->{
                        GenerateFactory.get(v.getType()).generate(tableInfo, v, generateReqVo.getModule());
                    });
                }
            });
        });

    }
}
